﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelReport
{
    class Report
    {
        object misValue;
        Excel.Application excelApp;
        Excel.Workbook workbook;

        public Report()
        {
            misValue = System.Reflection.Missing.Value;
            excelApp = new Excel.Application();
            // Make the object visible.
            excelApp.Visible = true;
            workbook = excelApp.Workbooks.Add(1);
        }

        //x-axis: algorithm and constant (try different constants), y-axis: hit ratio
        /*      public void OptimalReport()
              {
                  //reading from file
                  using (StreamReader sr = new StreamReader("../../../../DataBase/DataBase/DataBase/AlgorithmOptimalReport.txt"))
                  {
                      String line;
                      double hitRatio;
                      // Read data from u.data.
                      line = sr.ReadLine();
                      while ((line = sr.ReadLine()) != null)  // go through all lines in database
                      {
                          char[] delimiters = new char[] { '\t' };
                          String[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                          hitRatio = Convert.ToDouble(parts[0]);
                      }
                      try
                      {

                          Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
                          worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                          worksheet.Name = "Optimal";


                          // writing data to excel file
                          worksheet.Cells[1, 1] = "hit ratio";
                          worksheet.Cells[2, 1] = hitRatio;

                          Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
                          Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                          Excel.Chart chartPage = myChart.Chart;
                          var seriesCol = chartPage.SeriesCollection(misValue);
                          chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;

                          string sRow = Convert.ToString(numOfConstants);
                          Excel.Range chartRange1, chartRange2;
                          chartRange1 = worksheet.Range["A2", "A" + sRow];
                          chartRange2 = worksheet.Range["B2", "B" + sRow];
                          seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                          seriesCol(1).Name = a;
                          seriesCol(1).XValues = chartRange2;

                          dump(worksheet);

                      }
                      catch (Exception e)
                      {
                          Console.WriteLine("exception");
                      }
                  }
              }
      */
        //x-axis: algorithm and constant (try different constants), y-axis: hit ratio
        public void AlgorithmReport(string[] algoNames, int[] numOfConstants)
        {
            Console.WriteLine("here");
            int num = 0, iColumn = 1, series = 1;
            char cColumn = 'A';
            string col;
            Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
            worksheet = (Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = "Algos";
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
            Excel.Chart chartPage = myChart.Chart;
            var seriesCol = chartPage.SeriesCollection(misValue);
            chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;

            foreach (string a in algoNames)
            {
                if (a.CompareTo("Optimal") == 0)
                {
                    //reading from file
                    using (StreamReader sr = new StreamReader("../../../../DataBase/DataBase/AlgorithmOptimalReport.txt"))
                    {
                        String line;
                        double hitRatio = 0;
                        // Read data from u.data.
                        line = sr.ReadLine();
                        while ((line = sr.ReadLine()) != null)  // go through all lines in database
                        {
                            char[] delimiters = new char[] { '\t' };
                            String[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                            hitRatio = Convert.ToDouble(parts[0]);
                        }
                        try
                        {

                            //      Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
                            //      worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                            //      worksheet.Name = "Optimal";


                            // writing data to excel file
                            worksheet.Cells[1, iColumn] = a;
                            worksheet.Cells[2, iColumn] = "constants";
                            worksheet.Cells[3, iColumn] = hitRatio;
                            worksheet.Cells[4, iColumn] = hitRatio;
                            iColumn++;
                            worksheet.Cells[2, iColumn] = "hit ratio";
                            worksheet.Cells[3, iColumn] = 0;
                            worksheet.Cells[4, iColumn] = 10;
                            iColumn++;

                            /*        Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
                                    Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                                    Excel.Chart chartPage = myChart.Chart;
                                    var seriesCol = chartPage.SeriesCollection(misValue);
                                    chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
                            */

                            Excel.Range chartRange1, chartRange2;
                            col = cColumn.ToString();
                            chartRange1 = worksheet.Range[col + "3", col + "4"];
                            cColumn++;
                            col = cColumn.ToString();
                            chartRange2 = worksheet.Range[col + "3", col + "4"];
                            seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                            seriesCol(series).Name = a;
                            seriesCol(series).XValues = chartRange2;
                            cColumn++;
                            series++;

                            dump(worksheet);

                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("exception");
                        }
                    }
                }
                //reading from file
                else
                {
                    using (StreamReader sr = new StreamReader("../../../../DataBase/DataBase/Algorithm" + a + "Report.txt"))
                    {
                        String line;
                        int i = 0;
                        double[] constants = new double[numOfConstants[num]];
                        double[] hitRatio = new double[numOfConstants[num]];
                        // Read data from u.data.
                        line = sr.ReadLine();
                        while ((line = sr.ReadLine()) != null)  // go through all lines in database
                        {
                            char[] delimiters = new char[] { '\t' };
                            String[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                            constants[i] = Convert.ToDouble(parts[0]);
                            hitRatio[i] = Convert.ToDouble(parts[1]);
                            i++;
                        }
                        try
                        {

                            //        Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
                            //        worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                            //        worksheet.Name = a;

                            Console.WriteLine("here3");
                            // writing data to excel file
                            worksheet.Cells[1, iColumn] = a;
                            worksheet.Cells[2, iColumn + 1] = "constant";
                            worksheet.Cells[2, iColumn] = "hit ratio";
                            for (i = 3; i < numOfConstants[num] + 3; i++)
                            {
                                worksheet.Cells[i, iColumn] = hitRatio[i - 3];
                                worksheet.Cells[i, iColumn + 1] = constants[i - 3];
                            }
                            iColumn += 2;

                            /*         Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
                                     Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                                     Excel.Chart chartPage = myChart.Chart;
                                     var seriesCol = chartPage.SeriesCollection(misValue);
                                     chartPage.ChartType = Excel.XlChartType.xlXYScatterLinesNoMarkers;
                             */

                            string sRow = Convert.ToString(numOfConstants[num] + 2);
                            Excel.Range chartRange1, chartRange2;
                            col = cColumn.ToString();
                            chartRange1 = worksheet.Range[col + "3", col + sRow];
                            cColumn++;
                            col = cColumn.ToString();
                            chartRange2 = worksheet.Range[col + "3", col + sRow];
                            cColumn++;
                            seriesCol.add(chartRange1, misValue, misValue, misValue, misValue);
                            seriesCol(series).Name = a;
                            seriesCol(series).XValues = chartRange2;
                            series++;


                            dump(worksheet);

                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("exception");
                        }
                    }
                    num++;
                }
            }
        }

        // pie chart for the extent sort report
        public void ExtentSortReport()
        {
            //reading from file
            using (StreamReader sr = new StreamReader("../../../../DataBase/DataBase/extentSortReport.txt"))
            {
                String line;
                int i = 0;
                int[] results = new int[3];
                // Read data from u.data.
                while ((line = sr.ReadLine()) != null)  // go through all lines in database
                {
                    char[] delimiters = new char[] { ':' };
                    String[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    results[i] = Convert.ToInt32(parts[1]);
                    i++;
                }
                try
                {
                    //Excel.Worksheet worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                    Excel.Worksheet worksheet = (Excel.Worksheet)this.excelApp.Worksheets.Add(misValue, misValue, misValue, misValue);
                    worksheet = (Excel.Worksheet)workbook.ActiveSheet;
                    worksheet.Name = "extent sort";

                    // writing data to excel file
                    worksheet.Cells[1, 1] = "SSD";
                    worksheet.Cells[1, 2] = results[0];
                    worksheet.Cells[2, 1] = "FC";
                    worksheet.Cells[2, 2] = results[1];
                    worksheet.Cells[3, 1] = "SATA";
                    worksheet.Cells[3, 2] = results[2];

                    Excel.ChartObjects xlCharts = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
                    Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
                    Excel.Chart chartPage = myChart.Chart;

                    Excel.Range chartRange;
                    chartRange = worksheet.get_Range("A1", "B3");
                    chartPage.SetSourceData(chartRange, misValue);
                    chartPage.ChartType = Excel.XlChartType.xlPie;

                    // move chart to not hide data
                    chartRange = (Excel.Range)worksheet.Rows.get_Item(5, Type.Missing);
                    worksheet.Shapes.Item("Chart 1").Top = (float)(double)chartRange.Top;
                    chartRange = (Excel.Range)worksheet.Columns.get_Item(2, Type.Missing);
                    worksheet.Shapes.Item("Chart 1").Left = (float)(double)chartRange.Left;


                    dump(worksheet);

                }
                catch (Exception e)
                {
                    Console.WriteLine("exception");
                }

            }
        }

        private void dump(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine(ex);
            }
            finally
            {
                GC.Collect();
            }
        }

        public void save()
        {
            dump(workbook);
            dump(excelApp);
            string path = Directory.GetCurrentDirectory();
            workbook.SaveAs(path + "/Report.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            workbook.Close(true, misValue, misValue);
            excelApp.Quit();
        }
    }
}
